import { Fir } from "./fir";
import { Bullet } from "./bullet";
import { getRandomFloat } from "./unit";
import skin from "./../assets/YF22.png";
import bulletimage from "./../assets/ceil-bullet.png";
import { convertImageToBase64 } from "./unit";

let baseskin;
convertImageToBase64(skin).then((source) => {
  baseskin = source
})

let base64Bulletimage;
convertImageToBase64(bulletimage).then((source) => {
  base64Bulletimage = source
})

export class F35 extends Fir {
  constructor(canvas) {
    super();

    this.setName("绿机");
    this.setImage(baseskin);
    this.setColor("green");

    const oriWidth = parseInt(window.getComputedStyle(canvas).width)
    const oriHeight = parseInt(window.getComputedStyle(canvas).height)
    this.oriWidth = oriWidth
    this.oriHeight = oriHeight

    const baseWidth = (oriWidth < oriHeight ? oriWidth : oriHeight / 2)
    const width = getRandomFloat(baseWidth / 7, baseWidth / 5, 0);
    this.setWidth(width);
    this.setHeight(width * 0.3);
    this.setMaxSpeed(oriWidth / 300)
    this.setMaxPosition(oriWidth, oriHeight / 5)

    this.fact = Math.random() / (oriWidth * 5);

    setTimeout(() => {
      this.shooting();
    }, getRandomFloat(oriWidth * 2, oriWidth * 13.33, 0));
  }
  bullets = [];
  shooting() {
    const bullet = new Bullet();
    bullet.setName("F-001");
    bullet.setImage(base64Bulletimage);
    const width = (this.oriWidth < this.oriHeight ? this.oriWidth : this.oriHeight / 2) / 75;
    bullet.setWidth(width);
    bullet.setHeight(width * 4);
    bullet.setPosition(
      this.position[0] + this.width / 2,
      this.position[1] + this.height / 3
    );
    bullet.ondestory(() => {
      this.removeBullet(bullet)
      // bullet.audio.destory();
    })
    let count = 0;
    const handel = () => {
      bullet.position[1] = bullet.position[1] + count * 1.02;
      if (bullet.position[1] < innerHeight) {
        requestAnimationFrame(handel);
      } else {
        // 溢出可视区域，移除
        this.removeBullet(bullet)
      }
      count += this.oriHeight / 30000;
    };
    handel();
    this.bullets.push(bullet);
  }
  removeBullet(bullet) {
    const index = this.bullets.findIndex((item) => item === bullet);
    if (index > -1) {
      // bullet.audio.destory()
      this.bullets.splice(index, 1);
    }
  }
  isHit(bullet) {
    const my = {
      minx: this.position[0],
      maxx: this.position[0] + this.width,
      miny: this.position[1],
      maxy: this.position[1] + this.height,
    };
    const blt = {
      minx: bullet.position[0],
      maxx: bullet.position[0] + bullet.width,
      miny: bullet.position[1],
      maxy: bullet.position[1] + bullet.height,
    };

    if (my.maxx < blt.minx) return false;
    if (my.minx > blt.maxx) return false;
    if (my.maxy < blt.miny) return false;
    if (my.miny > blt.maxy) return false;

    return true;
  }
  go() {
    this.position[0] = -this.width;
    const arrow = Math.random() > 0.5 ? "up" : "down";
    const spaceY = Math.round(Math.random() * (this.maxPosition[1])) + 20;
    const stepX = getRandomFloat(0.5 * this.maxSpeed, this.maxSpeed);
    this.setStatus("runing");

    const hander = () => {
      if (this.position[0] > this.maxPosition[0] || this.status === "destory") {
        this.destory();
      } else {
        const newx = this.position[0] + stepX;
        if (arrow === "up") {
          const newy = -this.fact * Math.pow(newx, 2) + spaceY;
          this.setPosition(newx, newy)
        } else if (arrow === "down") {
          const newy = this.fact * Math.pow(newx, 2) + spaceY;
          this.setPosition(newx, newy)
        } else {
          this.setPosition(newx, this.position[1])
        }
  
        requestAnimationFrame(hander);
      }
    }

    hander();
    this.openFirVolce();
  }
}
